﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CsvViewer
{
    public partial class FilterConfigForm : Form
    {
        int initialHeight = 210;
        int h1 = 40;

        public List<FilterCondition> conditions = new List<FilterCondition>();

        public FilterConfigForm()
        {
            InitializeComponent();
        }


        public void AddCondition(FilterCondition condition = null)
        {
            ConditionConfigControl ccc = new ConditionConfigControl(condition)
            {
                Left = 0,
                Width = panel1.Width,
                Height = h1,
                Top = panel1.Controls.Count * h1
            };

            ccc.CloseButtonClicked += (s1, e1) =>
            {
                if (s1 is ConditionConfigControl control)
                {
                    panel1.Controls.Remove(control);
                    for (int i = 0; i < panel1.Controls.Count; i++)
                        panel1.Controls[i].Top = i * h1;
                    Height -= h1;
                }
            };

            panel1.Controls.Add(ccc);
            Height += h1;
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddCondition();
        }

        private void FilterConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            conditions.Clear();
            foreach (Control c in panel1.Controls)
                if (c is ConditionConfigControl cc)
                    conditions.Add(cc.ToFilterCondition());
        }

        private void FilterConfigForm_Load(object sender, EventArgs e)
        {
            foreach (FilterCondition c in conditions)
                AddCondition(c);
        }
    }
}
